﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vowei.Core.Models;

namespace Vowei.Data.Models
{
    public partial class PermissionSet : Permission, IContainer<Permission>
    {
        public PermissionSet()
        {
            ID = Guid.NewGuid();
        }
                
        public override bool Authorized(string permission)
        {
            if (base.Authorized(permission))
                return true;
            else
            {
                foreach (var perm in Children)
                {
                    if (perm.Authorized(permission))
                        return true;
                }

                return false;
            }
        }

        IQueryable<Permission> IContainer<Permission>.Children
        {
            get { return Children.AsQueryable<Permission>(); }
        }

        public void AddChild(Permission child)
        {
            Children.Add(child);
        }

        public void RemoveChild(Permission child)
        {
            Children.Remove(child);
        }


        public void ClearChildren()
        {
            Children.Clear();
        }
    }
}
